#ifndef VL_LOCALSTREAM_HPP
#define VL_LOCALSTREAM_HPP

#include "stream.hpp"
#include <utility>

namespace vl
{

struct local_buf : public device_buf
{
	local_buf()
	: dest(0)
	{
	}
	
	void connect(local_buf* dest_new)
	{
		dest = dest_new;
	}
	
	void disconnect()
	{
		dest = 0;
	}
	
	virtual size_t read(Byte* p, size_t size)
	{
		if(!dest)
			return device_buf::eof_value;
		return 0; // We can never read this way, the "remote" side inserts directly into our buffer
	}
	
	virtual size_t write(Byte const* p, size_t size)
	{
		if(!dest)
			return 0;
		dest->read_direct_fill(p, size);
		return size;
	}
	
	void do_close()
	{
		dest->disconnect();
		dest = 0;
	}

private:
	
	local_buf* dest;
	
};

std::pair<local_buf*, local_buf*> device_pair()
{
	local_buf* a = new local_buf;
	local_buf* b = new local_buf;
	a->connect(b);
	b->connect(a);
	return std::make_pair(a, b);
}

/* Use stream
struct localstream : public stream
{
	localstream(char const* addr, int port);
	
	socketstream(socket_buf* buf)
	: iobase(buf)
	{
	}
};*/

} // namespace vl

#endif // VL_LOCALSTREAM_HPP
